﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using TPCN.Common;

namespace TPCN.Admincp
{
    public partial class Phuongthucthanhtoan : System.Web.UI.Page
    {
        private static string PhuongThucThanhToanID = String.Empty;
        private static List<PhuongThucThanhToanObj> source;
        private static PhuongThucThanhToanObj obj;

        private const string UPDATE = "Cập nhật";
        private const string INSERT = "Thêm";
        private const string CMD_EDIT = "cmdEdit";
        private const string CMD_DELETE = "cmdDelete";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
                InitForm();
            }
        }

        private void InitForm()
        {
            EditFormPanel.Visible = false;
            GridViewPanel.Visible = true;
            ButtonPanel.Visible = true;
        }

        private void BindGridView()
        {
            source = new List<PhuongThucThanhToanObj>();
            source = PhuongThucThanhToanDAL.GetByAll();
            PhuongthucthanhtoanGridView.DataSource = source;
            PhuongthucthanhtoanGridView.DataBind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            TurnPanelOnOff();
            btnSave.Text = INSERT;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < PhuongthucthanhtoanGridView.Rows.Count; i++)
            {
                GridViewRow row = PhuongthucthanhtoanGridView.Rows[i];
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                    if (chk.Checked)
                    {
                        string ID = row.Cells[1].Text;
                        PhuongThucThanhToanDAL.Delete(ID);
                    }
                }
            }
            PhuongthucthanhtoanGridView.PageIndex = 0;
            BindGridView();
        }

        protected void PhuongthucthanhtoanGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            PhuongthucthanhtoanGridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void PhuongthucthanhtoanGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            PhuongThucThanhToanID = e.CommandArgument.ToString();
            obj = new PhuongThucThanhToanObj();
            obj = PhuongThucThanhToanDAL.GetByID(PhuongThucThanhToanID);
            switch (e.CommandName)
            {
                case CMD_EDIT:
                    FillValueToForm(obj);
                    btnSave.Text = UPDATE;
                    TurnPanelOnOff();
                    break;
                case CMD_DELETE:
                    PhuongThucThanhToanDAL.Delete(obj.PhuongthucthanhtoanID);
                    BindGridView();
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SetValueFromForm();
                switch (btnSave.Text)
                {
                    case UPDATE:
                        PhuongThucThanhToanDAL.Update(obj);
                        BindGridView();
                        break;
                    case INSERT:
                        PhuongThucThanhToanDAL.Insert(obj);
                        BindGridView();
                        break;
                }
                ControlHelper.ResetControl(EditFormPanel);
                TurnPanelOnOff();
                obj = null;
                Response.Redirect(Request.RawUrl);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            TurnPanelOnOff();
            ControlHelper.ResetControl(EditFormPanel);
            Response.Redirect(Request.RawUrl);
        }

        private void TurnPanelOnOff()
        {
            GridViewPanel.Visible = !GridViewPanel.Visible;
            EditFormPanel.Visible = !EditFormPanel.Visible;
            ButtonPanel.Visible = !ButtonPanel.Visible;
        }

        private void FillValueToForm(PhuongThucThanhToanObj obj)
        {
            txtTenphuongthucthanhtoan.Text = obj.TenPhuongthucthanhtoan;
            txtAnh.Text = obj.Bieutuong;
            imgAnh.ImageUrl = obj.Bieutuong;
        }

        protected void SetValueFromForm()
        {
            if (obj == null)
                obj = new PhuongThucThanhToanObj();
            obj.TenPhuongthucthanhtoan = txtTenphuongthucthanhtoan.Text;
            obj.Bieutuong = txtAnh.Text;
        }

        protected void btnSubmitUpload_Click(object sender, EventArgs e)
        {
            if (uploader.UploadedFiles.Count > 0)
            {
                txtAnh.Text = "~/Upload/" + uploader.UploadedFiles[0].GetName();
                imgAnh.ImageUrl = txtAnh.Text;
                setSelectedIndexcboType();
            }
        }

        protected void setSelectedIndexcboType()
        {
            string url = txtAnh.Text;
            if (url.StartsWith("~"))
            {
                cboType.SelectedIndex = 1;
            }
            else
                cboType.SelectedIndex = 0;
        }
    }
}